<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="handleSuccess" />
    <UserGoodsListDrawer @register="registerUserGoodsListDrawer" />
  </div>
</template>
<script lang="ts" setup name="exchangeGoods">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import Drawer from './components/drawer.vue';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { columns, searchFormSchema } from './data/data';
import { Api, createUserGoodsByBusExchangeGoods, cancelExchangeGoods } from './data/api';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import UserGoodsListDrawer from './components/ExchangeUserGoodsListDrawer.vue';

const { createMessage, createConfirm } = useMessage();
const [registerDrawer, { openDrawer }] = useDrawer();
const [registerUserGoodsListDrawer, { openDrawer: openUserGoodsListDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'exchangeGoods-template',
  tableProps: {
    //表格标题
    title: '电子金兑换列表',
    //主键字段
    rowKey: 'exchangeGoodsId',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      labelCol: { span: 6 },
      schemas: searchFormSchema,
      showExportButton: true,
      exportButtonOptions: {
        code: 'ACTION_exchangeGoodsExport',
      },
    },
    showActionColumn: true,
    //操作列配置
    actionColumn: {
      width: 240,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    clickToRowSelect: true,
    rowSelection: {
      type: 'radio',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows, selectedRow }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '查看物品',
      onClick: handleLookUserGoods.bind(null, record),
    },
    {
      label: '生成物品',
      onClick: handleCreateUserGoods.bind(null, record),
      ifShow: () => {
        return record.elePayStatus == 'PAY_1' && record.generateState == 0;
      },
      auth: 'ACTION_createUserGoods',
    },
    {
      label: '撤销兑换',
      onClick: handleCancelExchangeGoods.bind(null, record),
      ifShow: () => {
        return record.elePayStatus == 'PAY_1';
      },
      auth: 'ACTION_cancelExchangeGoods',
    },
  ];
}

function handleLookUserGoods(record: Recordable) {
  openUserGoodsListDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleCreateUserGoods(record: Recordable) {
  let exchangeGoodsId = record.exchangeGoodsId;
  createConfirm({
    iconType: 'info',
    title: '确认生成相应物品',
    content: '确认生成相应物品？',
    onOk: async () => {
      await createUserGoodsByBusExchangeGoods(exchangeGoodsId);
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}

function handleCancelExchangeGoods(record: Recordable) {
  let exchangeGoodsId = record.exchangeGoodsId;
  createConfirm({
    iconType: 'info',
    title: '确认撤销兑换相应物品',
    content: '确撤销兑换相应物品？',
    onOk: async () => {
      await cancelExchangeGoods(exchangeGoodsId);
      handleSuccess(true, {});
    },
    onCancel: async () => {},
  });
}
</script>
